<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="分组编码" prop="groupCode">
        <el-input
          v-model="queryParams.groupCode"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="分组名称" prop="groupName">
        <el-input
          v-model="queryParams.groupName"
          placeholder="请输入"
          clearable
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['business:device:deviceGroup:add']"
        >新建分组</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['business:device:deviceGroup:delete']"
        >删除</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="deviceGroupList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column type="index" label="序号" width="50"/>
      <el-table-column label="分组编码" align="center" prop="groupCode" width="200" />
      <el-table-column label="分组名称" align="center" prop="groupName" width="200" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="创建时间" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="更新时间" align="center" prop="updateTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.updateTime, '{y}-{m}-{d} {h}:{i}:{s}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作"  align="center" class-name="small-padding fixed-width"  fixed="right" width="280" >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-setting"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['business:device:deviceGroup:paramSet']"
          >参数配置</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-s-grid"
            @click="handleDeviceList(scope.row)"
            v-hasPermi="['business:device:deviceGroup:deviceList']"
          >设备列表</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-unlock"
            v-if="scope.row.groupStatus=='1'"
            @click="enableHandle(scope.row)"
            v-hasPermi="['business:device:deviceGroup:enable']"
          >启用</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-lock"
            v-if="scope.row.groupStatus=='0'"
            @click="disableHandle(scope.row)"
            v-hasPermi="['business:device:deviceGroup:enable']"
          >禁用</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            style="color: #f56c6c"
            @click="handleDelete(scope.row)"
            v-hasPermi="['business:device:deviceGroup:delete']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备管理对话框 -->
    <el-drawer size="40%"
               :title="title"
               :visible.sync="open"
               :wrapperClosable="false"
               direction="rtl"
               custom-class="form-drawer"
               ref="drawer"
    >
      <el-form ref="form" :model="form" :rules="rules"  class="drawer-box" style="padding:10px 30px;overflow-x: hidden;overflow-y: auto;height: calc(100% - 56px);border-bottom:1px solid #eee;">
        <div class="title">基本信息</div>
        <el-row :gutter="20">
          <el-col :span="12">
            <el-form-item label="分组编码" prop="groupCode">
              <el-input v-model="form.groupCode" :disabled="disabled" placeholder="请输入" />
            </el-form-item>
          </el-col>
          <el-col :span="12">
            <el-form-item label="分组名称" prop="groupName">
              <el-input v-model="form.groupName" :disabled="disabled" placeholder="请输入" />
            </el-form-item>
          </el-col>
        </el-row>
        <el-row :gutter="20">
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input :disabled="disabled" v-model="form.remark" type="textarea" :rows="4"/>
            </el-form-item>
          </el-col>
        </el-row>
        <div class="title" v-if="addOrEdit">告警设置</div>
        <el-row :gutter="20" v-if="addOrEdit">
          <el-col :span="24">
            <el-form-item label="告警方式">
              <el-checkbox v-model="form.phoneMsgAlarm" :disabled="disabled"  true-label="0" false-label="1">手机短信报警</el-checkbox>
              <el-checkbox v-model="form.phoneVoiceAlarm" :disabled="disabled"  true-label="0" false-label="1">手机语音呼叫</el-checkbox>
              <el-checkbox v-model="form.deviceBuzzAlarm" :disabled="disabled"  true-label="0" false-label="1">设备蜂鸣报警</el-checkbox>
            </el-form-item>
          </el-col>
        </el-row>
        <!--告警手机号设置-->
        <el-row :gutter="24" class="mb8" v-if="addOrEdit">
          <el-col :span="1.5">
            <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="addAlarmSetRow">新增</el-button>
            <el-button type="danger" plain icon="el-icon-delete" size="mini" :disabled="multipleAlarmPhone" @click="delAlarmPhoneRows" >移除</el-button>
          </el-col>
        </el-row>
        <el-row :gutter="20" style="padding-bottom: 15px;"  v-if="addOrEdit">
          <el-col :span="24">
            <el-table :data="form.alarmPhoneList"  stripe @selection-change="handleSelectionChangeAlarmPhone" style="width: 100%">
              <el-table-column  type="selection"  width="55"  align="center" />
              <el-table-column type="index"  label="序号"  width="50"></el-table-column>
              <el-table-column prop="phone" label="手机号">
                <template slot-scope="scope">
                  <el-input v-model="scope.row.phone" />
                </template>
              </el-table-column>
              <el-table-column prop="id" label="操作" width="150">
                <template slot-scope="scope">
                  <el-button size="mini" type="text" icon="el-icon-delete" style="color: #f56c6c" @click="delSignInRow(scope.$index)">移除</el-button>
                </template>
              </el-table-column>
            </el-table>
          </el-col>
        </el-row>
      </el-form>
      <div class="drawer-footer"  style="position: absolute;bottom:0px;display:flex;left:50%;padding:10px 0;transform:translate(-50%,0);">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button type="danger" plain @click="cancel">取 消</el-button>
      </div>
    </el-drawer>
  </div>
</template>

<script>
  import { listDeviceGroup, getDeviceGroup, addDeviceGroup, editDeviceGroup, delDeviceGroup,enable,disable} from "@/api/business/device/deviceGroup"
  export default {
    name: "DeviceGroup",
    dicts: ['group_status','alarm_type'],
    data() {
      return {
        addOrEdit:false,
        disabled:false,
        // 遮罩层
        loading: true,
        // 选中数组
        ids: [],
        // 非单个禁用
        single: true,
        // 非多个禁用
        multiple: true,
        // 显示搜索条件
        showSearch: true,
        // 总条数
        total: 0,
        // 设备分组列表数据
        deviceGroupList: [],
        // 弹出层标题
        title: "",
        // 是否显示弹出层
        open: false,
        // 查询参数
        queryParams: {
          pageNum: 1,
          pageSize: 10,
          groupCode: undefined,
          groupName: undefined,
        },
        // 表单参数
        form: {
          groupCode: undefined,
          groupName: undefined,
          sort: undefined,
          remark: undefined,
          phoneMsgAlarm:"1",
          phoneVoiceAlarm:"1",
          deviceBuzzAlarm:"1",
          alarmPhoneList:[],
        },
        // 表单校验
        rules: {
          groupCode: [
            { required: true, message: "分组编码不能为空", trigger: "blur" }
          ],
          groupName: [
            { required: true, message: "分组名称不能为空", trigger: "blur" }
          ],
        },
        multipleAlarmPhone: true,
        alarmPhoneSelectList:[],//手机号设置
      }
    },
    created() {
      this.getList()
    },
    methods: {
      /** 查询设备分组列表 */
      getList() {
        this.loading = true
        listDeviceGroup(this.queryParams).then(response => {
          this.deviceGroupList = response.rows
          this.total = response.total
          this.loading = false
        })

      },
      // 取消按钮
      cancel() {
        this.open = false
        this.reset()
      },
      // 表单重置
      reset() {
        this.form = {
          groupCode: undefined,
          groupName: undefined,
          sort: undefined,
          remark: undefined,
          phoneMsgAlarm:"1",
          phoneVoiceAlarm:"1",
          deviceBuzzAlarm:"1",
          alarmPhoneList:[],
        }
        this.resetForm("form")
      },
      /** 搜索按钮操作 */
      handleQuery() {
        this.queryParams.pageNum = 1
        this.getList()
      },
      /** 重置按钮操作 */
      resetQuery() {
        this.resetForm("queryForm")
        this.handleQuery()
      },
      // 多选框选中数据
      handleSelectionChange(selection) {
        this.ids = selection.map(item => item.id)
        this.single = selection.length!=1
        this.multiple = !selection.length
      },
      /** 新增按钮操作 */
      handleAdd() {
        this.reset()
        this.open = true
        this.addOrEdit=false
        this.title = "新增"
      },
      /** 修改按钮操作 */
      handleUpdate(row) {
        this.reset()
        this.addOrEdit=true
        const groupId = row.id || this.ids
        getDeviceGroup(groupId).then(response => {
          this.form = response.data
          this.open = true
          this.title = "编辑"
        })
      },
      /** 提交按钮 */
      submitForm: function() {
        this.$refs["form"].validate(valid => {
          if (valid) {
            if (this.form.id != undefined) {
              editDeviceGroup(this.form).then(response => {
                this.$modal.msgSuccess("编辑成功")
                this.open = false
                this.getList()
              })
            } else {
              addDeviceGroup(this.form).then(response => {
                this.$modal.msgSuccess("新增成功")
                this.open = false
                this.getList()
              })
            }
          }
        })
      },
      /** 删除按钮操作 */
      handleDelete(row) {
        const groupIds = row.id || this.ids
        this.$modal.confirm('是否确认删除该设备分组吗？').then(function() {
          return delDeviceGroup(groupIds)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("删除成功")
        }).catch(() => {})
      },
      /** 启用按钮操作 */
      enableHandle(row) {
        const groupIds = row.id || this.ids
        this.$modal.confirm('是否启用该设备分组？').then(function() {
          return enable(groupIds)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("启用成功")
        }).catch(() => {})
      },
      /** 禁用按钮操作 */
      disableHandle(row) {
        const groupIds = row.id || this.ids
        this.$modal.confirm('是否禁用该设备分组？').then(function() {
          return disable(groupIds)
        }).then(() => {
          this.getList()
          this.$modal.msgSuccess("禁用成功")
        }).catch(() => {})
      },
      /** 设备列表操作 */
      handleDeviceList: function(row) {
        const groupId = row.id
        //this.$router.push("device/deviceList/" + groupId)
        this.$router.push({path:'/device/deviceList',query:{groupId:groupId}})
        // this.$router.push({name:'device/deviceList',params:{groupId:groupId}})
      },
      /*告警设置*/
      addAlarmSetRow() {
        this.form.alarmPhoneList.push({groupId:this.form.id,phone:''})
      },
      // 优惠券选择
      handleSelectionChangeAlarmPhone(val){
        this.alarmPhoneSelectList = val
        this.multipleAlarmPhone=!val.length
      },
      delAlarmPhoneRows(){
        for (let i = 0 i < this.alarmPhoneSelectList.length i++) {
          const a = this.form.alarmPhoneList.findIndex(inner => { return inner.phone === this.alarmPhoneSelectList[i].phone })
          this.form.alarmPhoneList.splice(a, 1)
        }
      },
      delSignInRow(index) {
        /*if (this.form.alarmPhoneList.length == 1) {
          return
        }*/
        this.form.alarmPhoneList.splice(index,1)
      },
    }
  }
</script>
<style scoped lang="scss">
  .title{
    font-size: 16px;
    font-weight: bold;
    display: flex;
    align-items: center;
    color: #333;
    margin: 0;
    padding-bottom:10px;
    &::before{
      content: '';
      width: 5px;
      height: 16px;
      background: #1890FF;
      border-radius: 3px;
      display: inline-block;
      margin-right: 10px;
    }
  }
</style>
